<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>WebSocket Client</title>
    <script>
        // 当文档加载完毕时执行
        document.addEventListener('DOMContentLoaded', function () {
            // 创建WebSocket连接
            // const ws = new WebSocket('ws://localhost:8080/websocket');
            const ws = new WebSocket('ws://localhost:8080/websocket/x?a=1');

            // 连接打开时触发
            ws.onopen = function (event) {
                console.log('连接已打开');
                // 可以发送消息给服务端
                ws.send('Hello Server!');
            };

            // 接收到服务端消息时触发
            ws.onmessage = function (event) {
                console.log('接收到消息：', event.data);
                // 可以在页面上显示消息
                document.getElementById('messages').textContent += '\n' + event.data;
            };

            // 连接关闭时触发
            ws.onclose = function (event) {
                console.log('连接已关闭');
            };

            // 连接发生错误时触发
            ws.onerror = function (error) {
                console.error('发生错误：', error);
            };
            window.ws = ws;
        });
    </script>
</head>
<body>
<h1>WebSocket Client</h1>
<p id="messages"></p>
</body>
</html>